package com.burgstaller.aGTD.utils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.burgstaller.aGTD.R;
import com.burgstaller.aGTD.R.id;
import com.burgstaller.provider.AGtd;

public abstract class AbstractItemEditor extends Activity 
		implements OnClickListener {

	private static final int STATE_EDIT = 0;
	private static final int STATE_INSERT = 1;
	/**
	 * Index of the title column
	 */
	private static final int TITLE_INDEX = 1;
	private static final int MODIFIED_INDEX = 2;
	

	int m_state;

	Uri m_URI;
    

	/**
	 * Cursor which will provide access to the note whose title we are editing.
	 */
	protected Cursor m_cursor;
    
    /**
     * The EditText field from our UI. Keep track of this so we can extract the
     * text when we are finished.
     */
    EditText m_titleText;
    
    
	public AbstractItemEditor() {
		super();
	}

	@Override
	protected void onCreate(Bundle icicle) {
	    super.onCreate(icicle);
	
	    setContentView(getContentLayoutId());
	
	    Intent intent = getIntent();
	    // Get the uri of the note whose title we want to edit
	    Uri uri = intent.getData();
	    String action = intent.getAction();
	    if (action.equals(Intent.EDIT_ACTION)) {            
	    	// Requested to edit: set that state, and the data being edited.
	        m_state = STATE_EDIT;
	        m_URI = intent.getData();
	    } else if (action.equals(Intent.INSERT_ACTION)) {
	        // Requested to insert: set that state, and create a new entry
	        // in the container.
	        m_state = STATE_INSERT;
	        m_URI = getContentResolver().insert(intent.getData(), null);
	
	        // If we were unable to create a new note, then just finish
	        // this activity.  A RESULT_CANCELED will be sent back to the
	        // original activity if they requested a result.
	        if (m_URI == null) {
	            Log.e(AGtd.LOG_TAG, "Failed to insert new note into "
	                    + getIntent().getData());
	            finish();
	            return;
	        }
	
	        // The new entry was created, so assume all will end well and
	        // set the result to be returned.
	        setResult(RESULT_OK, m_URI.toString());
	
	    }
	
	    // Get a cursor to access the note
	    m_cursor = managedQuery(m_URI, getProjectionMap(), null, null);
	
	    // Set up click handlers for the text field and button
	    m_titleText = (EditText) this.findViewById(R.id.title);
	    m_titleText.setOnClickListener(this);
	}

	protected abstract int getContentLayoutId();

	@Override
	protected void onResume() {
	    super.onResume();
	
	    // Initialize the text with the title column from the cursor
	    if (m_cursor != null) {
	        m_cursor.first();
	        String title = m_cursor.getString(TITLE_INDEX);
	        m_titleText.setText(title);
	    }
	}

	@Override
	protected void onPause() {
	    super.onPause();
	
	    // Write the text back into the cursor 
	    if (m_cursor != null) {
	        String title = m_titleText.getText().toString();
	        if (title.length() <= 0) {
	        	deleteItem();
	        } else {
	            m_cursor.updateString(TITLE_INDEX, title);
	            m_cursor.updateLong(MODIFIED_INDEX, System.currentTimeMillis());
	            m_cursor.commitUpdates();
	        }
	    }
	}

	
	protected abstract String[] getProjectionMap();

	/**
	 * Take care of deleting a note.  Simply deletes the entry.
	 */
	final void deleteItem() {
	    if (m_cursor != null) {
	        m_titleText.setText("");
	        m_cursor.deleteRow();
	        m_cursor.deactivate();
	        m_cursor = null;
	    }
	}

	public void onClick(View arg0) {
		finish();
	}
}